package cn.ihelloworld.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.ihelloworld.bean.Catagory;
import cn.ihelloworld.bean.SysUser;
import cn.ihelloworld.service.ICatagoryService;
import cn.ihelloworld.service.ISysUserService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	private ICatagoryService catagoryService;
	@Autowired
	private ISysUserService sysUserService;
	
	@RequestMapping("/")
	public ModelAndView index(){
		return index(new ModelAndView());
	}
	@RequestMapping("")
	public ModelAndView index2(){
		return index(new ModelAndView());
	}
	@RequestMapping("index.html")
	public ModelAndView index(ModelAndView view){
		view.setViewName("admin/index");
		return view;
	}
	@RequestMapping("login.html")
	public ModelAndView login(ModelAndView view){
		view.setViewName("admin/login");
		return view;
	}
	@RequestMapping("login.do")
	public ModelAndView login(SysUser user,ModelAndView view,HttpServletRequest req,HttpServletResponse res){
		if(user==null||user.getAccount()==null||user.getPassword()==null){
			view.setViewName("admin/login");
			view.addObject("msg", "用户名或密码错误");
		}else{
			user=sysUserService.login(user);
			if(user!=null){
				req.getSession().setAttribute("admin", user);
				return new ModelAndView(new RedirectView("index.html"));
			}else{
				view.setViewName("admin/login");
				view.addObject("msg", "用户名或密码错误");
			}
		}
		
		return view;
	}
	@RequestMapping("articleadd.html")
	public ModelAndView articleAdd(ModelAndView view){
		view.setViewName("admin/articleadd");
		return view;
	}
	@RequestMapping("/common/catagory.do")
	@ResponseBody
	public List<Catagory> catagory(){
		List<Catagory> list=catagoryService.select(new Catagory());
		return list;
	}
}
